#!/usr/bin/python
import unittest
import tempfile
import sys
import os
from StringIO import StringIO

# Import app modules
tests_dir = os.path.dirname(sys.argv[0])
from uya.routerconf import general

import common

class TestGeneralModule(unittest.TestCase, common.BaseTestConfig):        

    root_tests_dir = tests_dir
    
    def test_hostname(self):
        def callback(config): 
            return general.hostname(config["general"]["hostname"])
        expected_modified_filenames = ['/etc/hostname', '/etc/hosts']            
        self.compare_filesystems(expected_modified_filenames, 
            [['hostname', 'dell']], callback)

    def test_hostname_get(self):
        config = common.init_filesystems_for_get(tests_dir)
        cgeneral = config["general"]
        general.hostname_get(cgeneral)
        self.assertEqual("originalhostname", cgeneral["hostname"])
                                                                                  
    def test_ntpdate(self):
        def callback(config): 
            return general.ntpdate(
                config["general"]["ntp_servers"], unprevileged=True)
        expected_modified_filenames = ['/etc/default/ntpdate']            
        self.compare_filesystems(expected_modified_filenames, [], callback)

    def test_ntpdate_get(self):
        config = common.init_filesystems_for_get(tests_dir)
        cgeneral = config["general"]
        general.ntpdate_get(cgeneral)
        self.assertEqual("1.server.com 2.server.com".split(), cgeneral["ntp_servers"])

    def test_dns(self):
        def callback(config): 
            dns = config["general"]
            return general.dns([dns["dns_primary"], 
                dns["dns_secondary"]])
        expected_modified_filenames = ['/etc/resolv.conf']            
        self.compare_filesystems(expected_modified_filenames, [], callback)

    def test_dns_get(self):
        config = common.init_filesystems_for_get(tests_dir)
        cgeneral = config["general"]
        general.dns_get(cgeneral)
        self.assertEqual("1.2.3.4", cgeneral["dns_primary"])
        self.assertEqual("5.6.7.8", cgeneral["dns_secondary"])
                                    
                
def suite():
    return unittest.TestLoader().loadTestsFromTestCase(TestGeneralModule)
                
if __name__ == '__main__':
    unittest.main()
